package leetcode_cn;

import java.util.Stack;

/**
 * 有效的括号
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-09-06 15:01
 */
public class IsValid {

    public boolean solution(String s){
        if (s == null || s.length() == 0) return true;

        Stack<Character> stack = new Stack<>();
        char[] array = s.toCharArray();
        for (int i = 0; i < array.length; i++) {
            if (stack.isEmpty()) {
                stack.push(array[i]);
                continue;
            }
            switch (array[i]){
                case ')':if (stack.peek() != '(') return false;
                            else stack.pop();break;
                case ']':if (stack.peek() != '[') return false;
                            else stack.pop();break;
                case '}':if (stack.peek() != '{') return false;
                            else stack.pop();break;
                default:stack.push(array[i]);break;
            }
        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new IsValid().solution("()[]{}"));
        System.out.println(new IsValid().solution("()"));
        System.out.println(new IsValid().solution("(]"));
    }
}
